<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html lang = "en">

<head>

<link rel="shortcut icon" href="http://algs4.cs.princeton.edu/favicon.ico">
<link rel="stylesheet"    href="http://algs4.cs.princeton.edu/java.css" type="text/css">

<title>StdDraw.java</title>

<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<meta NAME="AUTHOR" CONTENT="Robert Sedgewick and Kevin Wayne">
<meta NAME="DESCRIPTION" CONTENT="StdDraw code in Java">
<meta NAME="TITLE" CONTENT="StdDraw code in Java">
<meta NAME="KEYWORDS" CONTENT="StdDraw,java,programming,computer science,algorithm,data structure,program,code">
<meta NAME="ROBOTS" CONTENT="INDEX,FOLLOW">

</head>


<body>
<center><h1>StdDraw.java</h1></center><p><br>

Below is the syntax highlighted version of <a href = "StdDraw.java">StdDraw.java</a>.
<p><br>

<!-- Generator: GNU source-highlight 3.1.6
by Lorenzo Bettini
http://www.lorenzobettini.it
http://www.gnu.org/software/src-highlite -->
<pre><tt><span class="comment">/******************************************************************************</span>
<span class="comment"> *  Compilation:  javac StdDraw.java</span>
<span class="comment"> *  Execution:    java StdDraw</span>
<span class="comment"> *  Dependencies: none</span>
<span class="comment"> *</span>
<span class="comment"> *  Standard drawing library. This class provides a basic capability for</span>
<span class="comment"> *  creating drawings with your programs. It uses a simple graphics model that</span>
<span class="comment"> *  allows you to create drawings consisting of points, lines, and curves</span>
<span class="comment"> *  in a window on your computer and to save the drawings to a file.</span>
<span class="comment"> *</span>
<span class="comment"> *  Todo</span>
<span class="comment"> *  ----</span>
<span class="comment"> *    -  Add support for gradient fill, etc.</span>
<span class="comment"> *    -  Fix setCanvasSize() so that it can only be called once.</span>
<span class="comment"> *    -  On some systems, drawing a line (or other shape) that extends way</span>
<span class="comment"> *       beyond canvas (e.g., to infinity) dimensions does not get drawn.</span>
<span class="comment"> *</span>
<span class="comment"> *  Remarks</span>
<span class="comment"> *  -------</span>
<span class="comment"> *    -  don't use AffineTransform for rescaling since it inverts</span>
<span class="comment"> *       images and strings</span>
<span class="comment"> *</span>
<span class="comment"> ******************************************************************************/</span>

<span class="preproc">package</span><span class="normal"> edu</span><span class="symbol">.</span><span class="normal">princeton</span><span class="symbol">.</span><span class="normal">cs</span><span class="symbol">.</span><span class="normal">algs4</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">BasicStroke</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Color</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">FileDialog</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Font</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">FontMetrics</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Graphics2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Image</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">MediaTracker</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">RenderingHints</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">Toolkit</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">ActionListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">MouseMotionListener</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">KeyEvent</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">event</span><span class="symbol">.</span><span class="normal">KeyListener</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Arc2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Ellipse2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">GeneralPath</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Line2D</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">geom</span><span class="symbol">.</span><span class="normal">Rectangle2D</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">BufferedImage</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">DirectColorModel</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">awt</span><span class="symbol">.</span><span class="normal">image</span><span class="symbol">.</span><span class="normal">WritableRaster</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">File</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">io</span><span class="symbol">.</span><span class="normal">IOException</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">net</span><span class="symbol">.</span><span class="normal">URL</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">LinkedList</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">TreeSet</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> java</span><span class="symbol">.</span><span class="normal">util</span><span class="symbol">.</span><span class="normal">NoSuchElementException</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">imageio</span><span class="symbol">.</span><span class="normal">ImageIO</span><span class="symbol">;</span>

<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">ImageIcon</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JFrame</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JLabel</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenu</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuBar</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">JMenuItem</span><span class="symbol">;</span>
<span class="preproc">import</span><span class="normal"> javax</span><span class="symbol">.</span><span class="normal">swing</span><span class="symbol">.</span><span class="normal">KeyStroke</span><span class="symbol">;</span>

<span class="comment">/**</span>
<span class="comment"> *  The {</span><span class="type">@code</span><span class="comment"> StdDraw} class provides a basic capability for</span>
<span class="comment"> *  creating drawings with your programs. It uses a simple graphics model that</span>
<span class="comment"> *  allows you to create drawings consisting of points, lines, squares, </span>
<span class="comment"> *  circles, and other geometric shapes in a window on your computer and</span>
<span class="comment"> *  to save the drawings to a file. Standard drawing also includes</span>
<span class="comment"> *  facilities for text, color, pictures, and animation, along with</span>
<span class="comment"> *  user interaction via the keyboard and mouse.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Getting started.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  To use standard drawing, you must have </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.class</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> in your</span>
<span class="comment"> *  Java classpath. If you used our autoinstaller, you should be all set.</span>
<span class="comment"> *  Otherwise, download</span>
<span class="comment"> *  </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="normal"> </span><span class="symbol">=</span><span class="string"> "http://introcs.cs.princeton.edu/java/stdlib/StdDraw.java"</span><span class="keyword">&gt;</span><span class="comment">StdDraw.java</span><span class="keyword">&lt;/a&gt;</span>
<span class="comment"> *  and put a copy in your working directory.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Now, type the following short program into your editor:</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *   public class TestStdDraw {</span>
<span class="comment"> *       public static void main(String[] args) {</span>
<span class="comment"> *           StdDraw.setPenRadius(0.05);</span>
<span class="comment"> *           StdDraw.setPenColor(StdDraw.BLUE);</span>
<span class="comment"> *           StdDraw.point(0.5, 0.5);</span>
<span class="comment"> *           StdDraw.setPenColor(StdDraw.MAGENTA);</span>
<span class="comment"> *           StdDraw.line(0.2, 0.2, 0.8, 0.2);</span>
<span class="comment"> *       }</span>
<span class="comment"> *   }</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  If you compile and execute the program, you should see a window</span>
<span class="comment"> *  appear with a thick magenta line and a blue point.</span>
<span class="comment"> *  This program illustrates the two main types of methods in standard</span>
<span class="comment"> *  drawing</span><span class="preproc">&amp;mdash;</span><span class="comment">methods that draw geometric shapes and methods that</span>
<span class="comment"> *  control drawing parameters.</span>
<span class="comment"> *  The methods {</span><span class="type">@code</span><span class="comment"> StdDraw.line()} and {</span><span class="type">@code</span><span class="comment"> StdDraw.point()}</span>
<span class="comment"> *  draw lines and points; the methods {</span><span class="type">@code</span><span class="comment"> StdDraw.setPenRadius()}</span>
<span class="comment"> *  and {</span><span class="type">@code</span><span class="comment"> StdDraw.setPenColor()} control the line thickness and color.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Points and lines.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can draw points and line segments with the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #point(double x, double y)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #line(double x1, double y1, double x2, double y2)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates must be in the drawing area</span>
<span class="comment"> *  (between 0 and 1 and by default) or the points and lines will not be visible.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Squares, circles, rectangles, and ellipses.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can draw squares, circles, rectangles, and ellipses using</span>
<span class="comment"> *  the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #circle(double x, double y, double radius)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #ellipse(double x, double y, double semiMajorAxis, double semiMinorAxis)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #square(double x, double y, double radius)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #rectangle(double x, double y, double halfWidth, double halfHeight)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  All of these methods take as arguments the location and size of the shape.</span>
<span class="comment"> *  The location is always specified by the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates</span>
<span class="comment"> *  of its </span><span class="keyword">&lt;em&gt;</span><span class="comment">center</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment"> *  The size of a circle is specified by its radius and the size of an ellipse is</span>
<span class="comment"> *  specified by the lengths of its semi-major and semi-minor axes.</span>
<span class="comment"> *  The size of a square or rectangle is specified by its half-width or half-height.</span>
<span class="comment"> *  The convention for drawing squares and rectangles is parallel to those for</span>
<span class="comment"> *  drawing circles and ellipses, but may be unexpected to the uninitiated.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The methods above trace outlines of the given shapes. The following methods</span>
<span class="comment"> *  draw filled versions:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #filledCircle(double x, double y, double radius)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #filledEllipse(double x, double y, double semiMajorAxis, double semiMinorAxis)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #filledSquare(double x, double y, double radius)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #filledRectangle(double x, double y, double halfWidth, double halfHeight)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Circular arcs.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can draw circular arcs with the following method:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #arc(double x, double y, double radius, double angle1, double angle2)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The arc is from the circle centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) of the specified radius.</span>
<span class="comment"> *  The arc extends from angle1 to angle2. By convention, the angles are</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">polar</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> (counterclockwise angle from the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-axis)</span>
<span class="comment"> *  and represented in degrees. For example, {</span><span class="type">@code</span><span class="comment"> StdDraw.arc(0.0, 0.0, 1.0, 0, 90)}</span>
<span class="comment"> *  draws the arc of the unit circle from 3 o'clock (0 degrees) to 12 o'clock (90 degrees).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Polygons.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can draw polygons with the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #polygon(double[] x, double[] y)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #filledPolygon(double[] x, double[] y)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The points in the polygon are ({</span><span class="type">@code</span><span class="comment"> x[i]}, {</span><span class="type">@code</span><span class="comment"> y[i]}).</span>
<span class="comment"> *  For example, the following code fragment draws a filled diamond</span>
<span class="comment"> *  with vertices (0.1, 0.2), (0.2, 0.3), (0.3, 0.2), and (0.2, 0.1):</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *   double[] x = { 0.1, 0.2, 0.3, 0.2 };</span>
<span class="comment"> *   double[] y = { 0.2, 0.3, 0.2, 0.1 };</span>
<span class="comment"> *   StdDraw.filledPolygon(x, y);</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Pen size.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  The pen is circular, so that when you set the pen radius to </span><span class="keyword">&lt;em&gt;</span><span class="comment">r</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  and draw a point, you get a circle of radius </span><span class="keyword">&lt;em&gt;</span><span class="comment">r</span><span class="keyword">&lt;/em&gt;</span><span class="comment">. Also, lines are</span>
<span class="comment"> *  of thickness 2</span><span class="keyword">&lt;em&gt;</span><span class="comment">r</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> and have rounded ends. The default pen radius</span>
<span class="comment"> *  is 0.005 and is not affected by coordinate scaling. This default pen</span>
<span class="comment"> *  radius is about 1/200 the width of the default canvas, so that if</span>
<span class="comment"> *  you draw 100 points equally spaced along a horizontal or vertical line,</span>
<span class="comment"> *  you will be able to see individual circles, but if you draw 200 such</span>
<span class="comment"> *  points, the result will look like a line.</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setPenRadius(double radius)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  For example, {</span><span class="type">@code</span><span class="comment"> StdDraw.setPenRadius(0.025)} makes</span>
<span class="comment"> *  the thickness of the lines and the size of the points to be five times</span>
<span class="comment"> *  the 0.005 default.</span>
<span class="comment"> *  To draw points with the minimum possible radius (one pixel on typical</span>
<span class="comment"> *  displays), set the pen radius to 0.0.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Pen color.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  All geometric shapes (such as points, lines, and circles) are drawn using</span>
<span class="comment"> *  the current pen color. By default, it is black.</span>
<span class="comment"> *  You can change the pen color with the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setPenColor(int red, int green, int blue)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setPenColor(Color color)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first method allows you to specify colors using the RGB color system.</span>
<span class="comment"> *  This </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="normal"> </span><span class="symbol">=</span><span class="string"> "http://johndyer.name/lab/colorpicker/"</span><span class="keyword">&gt;</span><span class="comment">color picker</span><span class="keyword">&lt;/a&gt;</span>
<span class="comment"> *  is a convenient way to find a desired color.</span>
<span class="comment"> *  The second method allows you to specify colors using the</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> Color} data type that is discussed in Chapter 3. Until then,</span>
<span class="comment"> *  you can use this method with one of these predefined colors in standard drawing:</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> #BLACK}, {</span><span class="type">@link</span><span class="comment"> #BLUE}, {</span><span class="type">@link</span><span class="comment"> #CYAN}, {</span><span class="type">@link</span><span class="comment"> #DARK_GRAY}, {</span><span class="type">@link</span><span class="comment"> #GRAY},</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> #GREEN}, {</span><span class="type">@link</span><span class="comment"> #LIGHT_GRAY}, {</span><span class="type">@link</span><span class="comment"> #MAGENTA}, {</span><span class="type">@link</span><span class="comment"> #ORANGE},</span>
<span class="comment"> *  {</span><span class="type">@link</span><span class="comment"> #PINK}, {</span><span class="type">@link</span><span class="comment"> #RED}, {</span><span class="type">@link</span><span class="comment"> #WHITE}, and {</span><span class="type">@link</span><span class="comment"> #YELLOW}.</span>
<span class="comment"> *  For example, {</span><span class="type">@code</span><span class="comment"> StdDraw.setPenColor(StdDraw.MAGENTA)} sets the</span>
<span class="comment"> *  pen color to magenta.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Canvas size.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  By default, all drawing takes places in a 512-by-512 canvas.</span>
<span class="comment"> *  The canvas does not include the window title or window border.</span>
<span class="comment"> *  You can change the size of the canvas with the following method:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setCanvasSize(int width, int height)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  This sets the canvas size to be </span><span class="keyword">&lt;em&gt;</span><span class="comment">width</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-by-</span><span class="keyword">&lt;em&gt;</span><span class="comment">height</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> pixels.</span>
<span class="comment"> *  It also erases the current drawing and resets the coordinate system,</span>
<span class="comment"> *  pen radius, pen color, and font back to their default values.</span>
<span class="comment"> *  Ordinarly, this method is called once, at the very beginning of a program.</span>
<span class="comment"> *  For example, {</span><span class="type">@code</span><span class="comment"> StdDraw.setCanvasSize(800, 800)}</span>
<span class="comment"> *  sets the canvas size to be 800-by-800 pixels.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Canvas scale and coordinate system.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  By default, all drawing takes places in the unit square, with (0, 0) at</span>
<span class="comment"> *  lower left and (1, 1) at upper right. You can change the default</span>
<span class="comment"> *  coordinate system with the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setXscale(double xmin, double xmax)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setYscale(double ymin, double ymax)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setScale(double min, double max)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The arguments are the coordinates of the minimum and maximum </span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- or </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates that will appear in the canvas.</span>
<span class="comment"> *  For example, if you  wish to use the default coordinate system but</span>
<span class="comment"> *  leave a small margin, you can call {</span><span class="type">@code</span><span class="comment"> StdDraw.setScale(-.05, 1.05)}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  These methods change the coordinate system for subsequent drawing</span>
<span class="comment"> *  commands; they do not affect previous drawings.</span>
<span class="comment"> *  These methods do not change the canvas size; so, if the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-</span>
<span class="comment"> *  and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scales are different, squares will become rectangles</span>
<span class="comment"> *  and circles will become ellipsoidal.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Text.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following methods to annotate your drawings with text:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #text(double x, double y, String text)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #text(double x, double y, String text, double degrees)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #textLeft(double x, double y, String text)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #textRight(double x, double y, String text)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first two methods write the specified text in the current font,</span>
<span class="comment"> *  centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment"> *  The second method allows you to rotate the text.</span>
<span class="comment"> *  The last two methods either left- or right-align the text at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The default font is a Sans Serif font with point size 16.</span>
<span class="comment"> *  You can use the following method to change the font:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #setFont(Font font)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  You use the {</span><span class="type">@link</span><span class="comment"> Font} data type to specify the font. This allows you to</span>
<span class="comment"> *  choose the face, size, and style of the font. For example, the following</span>
<span class="comment"> *  code fragment sets the font to Arial Bold, 60 point.</span>
<span class="comment"> *  </span><span class="keyword">&lt;pre&gt;</span>
<span class="comment"> *   Font font = new Font("Arial", Font.BOLD, 60);</span>
<span class="comment"> *   StdDraw.setFont(font);</span>
<span class="comment"> *   StdDraw.text(0.5, 0.5, "Hello, World");</span>
<span class="comment"> *  </span><span class="keyword">&lt;/pre&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Images.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following methods to add images to your drawings:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #picture(double x, double y, String filename)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #picture(double x, double y, String filename, double degrees)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #picture(double x, double y, String filename, double width)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #picture(double x, double y, String filename, double width, double degrees)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  These methods draw the specified image, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment"> *  The supported image formats are JPEG, PNG, and GIF.</span>
<span class="comment"> *  The image will display at its native size, independent of the coordinate system.</span>
<span class="comment"> *  Optionally, you can rotate the image a specified number of degrees counterclockwise</span>
<span class="comment"> *  or rescale it to fit inside a width-by-height pixel bounding box.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Saving to a file.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You save your image to a file using the </span><span class="keyword">&lt;em&gt;</span><span class="comment">File -&gt; Save</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> menu option.</span>
<span class="comment"> *  You can also save a file programatically using the following method:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #save(String filename)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The supported image formats are JPEG and PNG. The filename must have either the</span>
<span class="comment"> *  extension .jpg or .png.</span>
<span class="comment"> *  We recommend using PNG for drawing that consist solely of geometric shapes and JPEG </span>
<span class="comment"> *  for drawings that contains pictures.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Clearing the canvas.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  To clear the entire drawing canvas, you can use the following methods:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #clear()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #clear(Color color)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first method clears the canvas to white; the second method</span>
<span class="comment"> *  allows you to specify a color of your choice. For example,</span>
<span class="comment"> *  {</span><span class="type">@code</span><span class="comment"> StdDraw.clear(StdDraw.LIGHT_GRAY)} clears the canvas to a shade</span>
<span class="comment"> *  of gray. Most often, these two methods are used in conjunction with animation mode.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Animations.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  Animation mode is one of the trickier features of standard drawing.</span>
<span class="comment"> *  The following two methods control the way in which objects are drawn:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #show()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #show(int t)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  By default, animation mode is off, which means that as soon as you</span>
<span class="comment"> *  call a drawing</span>
<span class="comment"> *  method</span><span class="preproc">&amp;mdash;</span><span class="comment">such as {</span><span class="type">@code</span><span class="comment"> point()} or {</span><span class="type">@code</span><span class="comment"> line()}</span><span class="preproc">&amp;mdash;</span><span class="comment">the</span>
<span class="comment"> *  results appear on the screen. {</span><span class="type">@code</span><span class="comment"> StdDraw.show()} turns off</span>
<span class="comment"> *  animation mode.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  You can call {</span><span class="type">@link</span><span class="comment"> #show(int t)} to turn on animation mode. This</span>
<span class="comment"> *  defers all drawing to the screen until you are aready to display them.</span>
<span class="comment"> *  Once you are ready to display them,</span>
<span class="comment"> *  you call {</span><span class="type">@link</span><span class="comment"> #show(int t)} again, which transfer the offscreen</span>
<span class="comment"> *  drawing to the screen and waits for the specified number of milliseconds.</span>
<span class="comment"> *  In conjuction with {</span><span class="type">@link</span><span class="comment"> #clear()}, you can create the illusion</span>
<span class="comment"> *  of movement by iterating the following three steps:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Clear the background canvas.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Draw geometric objects.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Show the drawing and wait for a short while.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Waiting for a short while is essential; otherwise, the drawing will appear</span>
<span class="comment"> *  and disappear so quickly that your animation will flicker.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  Here is a simple example of an animation:</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Keyboard and mouse inputs.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  Standard drawing has very basic support for keyboard and mouse input.</span>
<span class="comment"> *  It is much less powerful than most user interface libraries provide, but also much simpler.</span>
<span class="comment"> *  You can use the following methods to intercept mouse events:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #mousePressed()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #mouseX()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #mouseY()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  The first method tells you whether a mouse button is currently being pressed.</span>
<span class="comment"> *  The last two methods tells you the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates of the mouse's</span>
<span class="comment"> *  current position, using the same coordinate system as the canvas (the unit square, by default).</span>
<span class="comment"> *  You should use these methods in an animation loop that waits a short while before trying</span>
<span class="comment"> *  to poll the mouse for its current state.</span>
<span class="comment"> *  You can use the following methods to intercept keyboard events:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #hasNextKeyTyped()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #nextKeyTyped()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #isKeyPressed(int keycode)}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  If the user types lots of keys, they will be saved in a list until you process them.</span>
<span class="comment"> *  The first method tells you whether the user has typed a key (that your program has</span>
<span class="comment"> *  not yet processed).</span>
<span class="comment"> *  The second method returns the next key that the user typed (that your program has</span>
<span class="comment"> *  not yet processed) and removes it from the list of saved keystrokes.</span>
<span class="comment"> *  The third method tells you whether a key is currently being pressed.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Accessing control parameters.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  You can use the following methods to access the current pen color, pen radius,</span>
<span class="comment"> *  and font:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #getPenColor()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #getPenRadius()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> {</span><span class="type">@link</span><span class="comment"> #getFont()}</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  These methods are useful when you want to temporarily change a</span>
<span class="comment"> *  control parameter and reset it back to its original value.</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Corner cases.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  To avoid clutter, the API doesn't explicitly refer to arguments that are</span>
<span class="comment"> *  null, infinity, or NaN.</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Any method that is passed a {</span><span class="type">@code</span><span class="comment"> null} argument will throw a</span>
<span class="comment"> *       {</span><span class="type">@link</span><span class="comment"> NullPointerException}.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Except as noted in the APIs, drawing an object outside (or partly outside)</span>
<span class="comment"> *       the canvas is permitted</span><span class="preproc">&amp;mdash;</span><span class="comment">however, only the part of the object that</span>
<span class="comment"> *       appears inside the canvas will be visible.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Except as noted in the APIs, all methods accept {</span><span class="type">@link</span><span class="comment"> Double#NaN},</span>
<span class="comment"> *       {</span><span class="type">@link</span><span class="comment"> Double#POSITIVE_INFINITY}, and {</span><span class="type">@link</span><span class="comment"> Double#NEGATIVE_INFINITY}</span>
<span class="comment"> *       as arugments. An object drawn with an </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- or </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate</span>
<span class="comment"> *       that is NaN will behave as if it is outside the canvas, and will not be visible.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Performance tricks.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  Standard drawing is capable of drawing large amounts of data.</span>
<span class="comment"> *  Here are a few tricks and tips:</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Use </span><span class="keyword">&lt;em&gt;</span><span class="comment">animation mode</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> for static drawing with a large</span>
<span class="comment"> *       number of objects.</span>
<span class="comment"> *       That is, call {</span><span class="type">@code</span><span class="comment"> StdDraw.show(0)} before</span>
<span class="comment"> *       and after the sequence of drawing commands.</span>
<span class="comment"> *       The bottleneck operation is not drawing the geometric</span>
<span class="comment"> *       shapes but rather drawing them to the screen. By using animation</span>
<span class="comment"> *       mode, you draw all of the shapes to an offscreen buffer, then copy</span>
<span class="comment"> *       them all at once to the screen.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> When using </span><span class="keyword">&lt;em&gt;</span><span class="comment">animation mode</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, call {</span><span class="type">@code</span><span class="comment"> show()}</span>
<span class="comment"> *       only once per frame, not after drawing each object.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> If you call {</span><span class="type">@code</span><span class="comment"> picture()} multiple times with the same filename,</span>
<span class="comment"> *       Java will cache the image, so you do not incur the cost of reading</span>
<span class="comment"> *       from a file each time.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Do not call {</span><span class="type">@code</span><span class="comment"> setFont()} in an animation loop (unless you really</span>
<span class="comment"> *       need to change the font in each iteration). It can cause flicker.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Known bugs and issues.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> The {</span><span class="type">@code</span><span class="comment"> picture()} methods may not draw the portion of the image that is</span>
<span class="comment"> *       inside the canvas if the center point (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) is outside the</span>
<span class="comment"> *       canvas.</span>
<span class="comment"> *       This bug appears only on some systems.</span>
<span class="comment"> *  </span><span class="keyword">&lt;li&gt;</span><span class="comment"> Some methods may not draw the portion of the geometric object that is inside the</span>
<span class="comment"> *       canvas if the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- or </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates are infinite.</span>
<span class="comment"> *       This bug appears only on some systems.</span>
<span class="comment"> *  </span><span class="keyword">&lt;/ul&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;p&gt;</span>
<span class="comment"> *  </span><span class="keyword">&lt;b&gt;</span><span class="comment">Reference.</span><span class="keyword">&lt;/b&gt;</span>
<span class="comment"> *  For additional documentation,</span>
<span class="comment"> *  see </span><span class="keyword">&lt;a</span><span class="normal"> </span><span class="type">href</span><span class="symbol">=</span><span class="string">"http://introcs.cs.princeton.edu/15inout"</span><span class="keyword">&gt;</span><span class="comment">Section 1.5</span><span class="keyword">&lt;/a&gt;</span><span class="comment"> of</span>
<span class="comment"> *  </span><span class="keyword">&lt;em&gt;</span><span class="comment">Introduction to Programming in Java: An Interdisciplinary Approach</span><span class="keyword">&lt;/em&gt;</span>
<span class="comment"> *  by Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Robert Sedgewick</span>
<span class="comment"> *  </span><span class="type">@author</span><span class="comment"> Kevin Wayne</span>
<span class="comment"> */</span>
<span class="keyword">public</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="keyword">class</span><span class="normal"> </span><span class="classname">StdDraw</span><span class="normal"> </span><span class="keyword">implements</span><span class="normal"> ActionListener</span><span class="symbol">,</span><span class="normal"> MouseListener</span><span class="symbol">,</span><span class="normal"> MouseMotionListener</span><span class="symbol">,</span><span class="normal"> KeyListener </span><span class="cbracket">{</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color black.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BLACK </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">BLACK</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color blue.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BLUE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">BLUE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color cyan.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> CYAN </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">CYAN</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color dark gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DARK_GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">DARK_GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color green.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> GREEN  </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">GREEN</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color light gray.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> LIGHT_GRAY </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">LIGHT_GRAY</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color magenta.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> MAGENTA </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">MAGENTA</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color orange.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> ORANGE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">ORANGE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color pink.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> PINK </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">PINK</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color red.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> RED </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">RED</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color white.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> WHITE </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">WHITE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     *  The color yellow.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> YELLOW </span><span class="symbol">=</span><span class="normal"> Color</span><span class="symbol">.</span><span class="normal">YELLOW</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of blue used in </span><span class="keyword">&lt;em&gt;</span><span class="comment">Introduction to Programming in Java</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * It is Pantone 300U. The RGB values are approximately (9, 90, 166).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_BLUE </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">9</span><span class="symbol">,</span><span class="normal"> </span><span class="number">90</span><span class="symbol">,</span><span class="normal"> </span><span class="number">166</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of light blue used in </span><span class="keyword">&lt;em&gt;</span><span class="comment">Introduction to Programming in Java</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * The RGB values are approximately (103, 198, 243).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_LIGHT_BLUE </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">103</span><span class="symbol">,</span><span class="normal"> </span><span class="number">198</span><span class="symbol">,</span><span class="normal"> </span><span class="number">243</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Shade of red used in </span><span class="keyword">&lt;em&gt;</span><span class="comment">Algorithms, 4th edition</span><span class="keyword">&lt;/em&gt;</span><span class="comment">.</span>
<span class="comment">     * It is Pantone 1805U. The RGB values are approximately (150, 35, 31).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> BOOK_RED </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="number">150</span><span class="symbol">,</span><span class="normal"> </span><span class="number">35</span><span class="symbol">,</span><span class="normal"> </span><span class="number">31</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// default colors</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DEFAULT_PEN_COLOR   </span><span class="symbol">=</span><span class="normal"> BLACK</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> DEFAULT_CLEAR_COLOR </span><span class="symbol">=</span><span class="normal"> WHITE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// current pen color</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> penColor</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// default canvas size is DEFAULT_SIZE-by-DEFAULT_SIZE</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">int</span><span class="normal"> DEFAULT_SIZE </span><span class="symbol">=</span><span class="normal"> </span><span class="number">512</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> width  </span><span class="symbol">=</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">int</span><span class="normal"> height </span><span class="symbol">=</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// default pen radius</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_PEN_RADIUS </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.002</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// current pen radius</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> penRadius</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// show we draw immediately or wait until next show?</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// boundary of drawing canvas, 0% border</span>
<span class="normal">    </span><span class="comment">// private static final double BORDER = 0.05;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> BORDER </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.00</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_XMIN </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_XMAX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_YMIN </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="type">double</span><span class="normal"> DEFAULT_YMAX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">1.0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> xmin</span><span class="symbol">,</span><span class="normal"> ymin</span><span class="symbol">,</span><span class="normal"> xmax</span><span class="symbol">,</span><span class="normal"> ymax</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// for synchronization</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> mouseLock </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Object</span><span class="symbol">();</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Object</span><span class="normal"> keyLock </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Object</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">// default font</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="keyword">final</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> DEFAULT_FONT </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">Font</span><span class="symbol">(</span><span class="string">"SansSerif"</span><span class="symbol">,</span><span class="normal"> Font</span><span class="symbol">.</span><span class="normal">PLAIN</span><span class="symbol">,</span><span class="normal"> </span><span class="number">16</span><span class="symbol">);</span>

<span class="normal">    </span><span class="comment">// current font</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> font</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// double buffered graphics</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">BufferedImage</span><span class="normal"> offscreenImage</span><span class="symbol">,</span><span class="normal"> onscreenImage</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Graphics2D</span><span class="normal"> offscreen</span><span class="symbol">,</span><span class="normal"> onscreen</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// singleton for callbacks: avoids generation of extra .class files</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">StdDraw</span><span class="normal"> std </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">StdDraw</span><span class="symbol">();</span>

<span class="normal">    </span><span class="comment">// the frame for drawing to the screen</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">JFrame</span><span class="normal"> frame</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// mouse state</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> mouseX </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> mouseY </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span>

<span class="normal">    </span><span class="comment">// queue of typed key characters</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">LinkedList&lt;Character&gt;</span><span class="normal"> keysTyped </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> LinkedList</span><span class="symbol">&lt;</span><span class="normal">Character</span><span class="symbol">&gt;();</span>

<span class="normal">    </span><span class="comment">// set of key codes currently pressed down</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">TreeSet&lt;Integer&gt;</span><span class="normal"> keysDown </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> TreeSet</span><span class="symbol">&lt;</span><span class="normal">Integer</span><span class="symbol">&gt;();</span>

<span class="normal">    </span><span class="comment">// time in milliseconds (from currentTimeMillis()) when we can draw again</span>
<span class="normal">    </span><span class="comment">// used to control the frame rate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">long</span><span class="normal"> nextDraw </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span><span class="normal">  </span>

<span class="normal">    </span><span class="comment">// singleton pattern: client can't instantiate</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="function">StdDraw</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">// static initializer</span>
<span class="normal">    </span><span class="keyword">static</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">init</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the canvas (drawing area) to be 512-by-512 pixels.</span>
<span class="comment">     * This also erases the current drawing and resets the coordinate system,</span>
<span class="comment">     * pen radius, pen color, and font back to their default values.</span>
<span class="comment">     * Ordinarly, this method is called once, at the very beginning</span>
<span class="comment">     * of a program.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setCanvasSize</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setCanvasSize</span><span class="symbol">(</span><span class="normal">DEFAULT_SIZE</span><span class="symbol">,</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the canvas (drawing area) to be </span><span class="keyword">&lt;em&gt;</span><span class="comment">width</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-by-</span><span class="keyword">&lt;em&gt;</span><span class="comment">height</span><span class="keyword">&lt;/em&gt;</span><span class="comment"> pixels.</span>
<span class="comment">     * This also erases the current drawing and resets the coordinate system,</span>
<span class="comment">     * pen radius, pen color, and font back to their default values.</span>
<span class="comment">     * Ordinarly, this method is called once, at the very beginning</span>
<span class="comment">     * of a program.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  canvasWidth the width as a number of pixels</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  canvasHeight the height as a number of pixels</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless both {</span><span class="type">@code</span><span class="comment"> width} and</span>
<span class="comment">     *         {</span><span class="type">@code</span><span class="comment"> height} are positive</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setCanvasSize</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> canvasWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> canvasHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">width </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> height </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"width and height must be positive"</span><span class="symbol">);</span>
<span class="normal">        width </span><span class="symbol">=</span><span class="normal"> canvasWidth</span><span class="symbol">;</span>
<span class="normal">        height </span><span class="symbol">=</span><span class="normal"> canvasHeight</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">init</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// init</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">init</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">frame </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> frame</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">        frame </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JFrame</span><span class="symbol">();</span>
<span class="normal">        offscreenImage </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_ARGB</span><span class="symbol">);</span>
<span class="normal">        onscreenImage  </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> BufferedImage</span><span class="symbol">.</span><span class="normal">TYPE_INT_ARGB</span><span class="symbol">);</span>
<span class="normal">        offscreen </span><span class="symbol">=</span><span class="normal"> offscreenImage</span><span class="symbol">.</span><span class="function">createGraphics</span><span class="symbol">();</span>
<span class="normal">        onscreen  </span><span class="symbol">=</span><span class="normal"> onscreenImage</span><span class="symbol">.</span><span class="function">createGraphics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setXscale</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setYscale</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">DEFAULT_CLEAR_COLOR</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setPenRadius</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setFont</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">clear</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// add antialiasing</span>
<span class="normal">        </span><span class="usertype">RenderingHints</span><span class="normal"> hints </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">RenderingHints</span><span class="symbol">(</span><span class="normal">RenderingHints</span><span class="symbol">.</span><span class="normal">KEY_ANTIALIASING</span><span class="symbol">,</span>
<span class="normal">                                                  RenderingHints</span><span class="symbol">.</span><span class="normal">VALUE_ANTIALIAS_ON</span><span class="symbol">);</span>
<span class="normal">        hints</span><span class="symbol">.</span><span class="function">put</span><span class="symbol">(</span><span class="normal">RenderingHints</span><span class="symbol">.</span><span class="normal">KEY_RENDERING</span><span class="symbol">,</span><span class="normal"> RenderingHints</span><span class="symbol">.</span><span class="normal">VALUE_RENDER_QUALITY</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">addRenderingHints</span><span class="symbol">(</span><span class="normal">hints</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// frame stuff</span>
<span class="normal">        </span><span class="usertype">ImageIcon</span><span class="normal"> icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">onscreenImage</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">JLabel</span><span class="normal"> draw </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JLabel</span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">);</span>

<span class="normal">        draw</span><span class="symbol">.</span><span class="function">addMouseListener</span><span class="symbol">(</span><span class="normal">std</span><span class="symbol">);</span>
<span class="normal">        draw</span><span class="symbol">.</span><span class="function">addMouseMotionListener</span><span class="symbol">(</span><span class="normal">std</span><span class="symbol">);</span>

<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setContentPane</span><span class="symbol">(</span><span class="normal">draw</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">addKeyListener</span><span class="symbol">(</span><span class="normal">std</span><span class="symbol">);</span><span class="normal">    </span><span class="comment">// JLabel cannot get keyboard focus</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setResizable</span><span class="symbol">(</span><span class="keyword">false</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setDefaultCloseOperation</span><span class="symbol">(</span><span class="normal">JFrame</span><span class="symbol">.</span><span class="normal">EXIT_ON_CLOSE</span><span class="symbol">);</span><span class="normal">            </span><span class="comment">// closes all windows</span>
<span class="normal">        </span><span class="comment">// frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);      // closes only current window</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setTitle</span><span class="symbol">(</span><span class="string">"Standard Draw"</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setJMenuBar</span><span class="symbol">(</span><span class="function">createMenuBar</span><span class="symbol">());</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">pack</span><span class="symbol">();</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">requestFocusInWindow</span><span class="symbol">();</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// create the menu bar (changed to private)</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">JMenuBar</span><span class="normal"> </span><span class="function">createMenuBar</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">JMenuBar</span><span class="normal"> menuBar </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuBar</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">JMenu</span><span class="normal"> menu </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenu</span><span class="symbol">(</span><span class="string">"File"</span><span class="symbol">);</span>
<span class="normal">        menuBar</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menu</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">JMenuItem</span><span class="normal"> menuItem1 </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">JMenuItem</span><span class="symbol">(</span><span class="string">" Save...   "</span><span class="symbol">);</span>
<span class="normal">        menuItem1</span><span class="symbol">.</span><span class="function">addActionListener</span><span class="symbol">(</span><span class="normal">std</span><span class="symbol">);</span>
<span class="normal">        menuItem1</span><span class="symbol">.</span><span class="function">setAccelerator</span><span class="symbol">(</span><span class="normal">KeyStroke</span><span class="symbol">.</span><span class="function">getKeyStroke</span><span class="symbol">(</span><span class="normal">KeyEvent</span><span class="symbol">.</span><span class="normal">VK_S</span><span class="symbol">,</span>
<span class="normal">                                Toolkit</span><span class="symbol">.</span><span class="function">getDefaultToolkit</span><span class="symbol">().</span><span class="function">getMenuShortcutKeyMask</span><span class="symbol">()));</span>
<span class="normal">        menu</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">menuItem1</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> menuBar</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  User and screen coordinate systems.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to be the default (between 0.0 and 1.0).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setXscale</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setXscale</span><span class="symbol">(</span><span class="normal">DEFAULT_XMIN</span><span class="symbol">,</span><span class="normal"> DEFAULT_XMAX</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to be the default (between 0.0 and 1.0).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setYscale</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setYscale</span><span class="symbol">(</span><span class="normal">DEFAULT_YMIN</span><span class="symbol">,</span><span class="normal"> DEFAULT_YMAX</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to be the default</span>
<span class="comment">     * (between 0.0 and 1.0).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setScale</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setXscale</span><span class="symbol">();</span>
<span class="normal">        </span><span class="function">setYscale</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to the specified range.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  min the minimum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  max the maximum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> (max == min)}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setXscale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> size </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">-</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">size </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"the min and max are the same"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            xmin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">            xmax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to the specified range.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  min the minimum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  max the maximum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> (max == min)}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setYscale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> size </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">-</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">size </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"the min and max are the same"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            ymin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">            ymax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets both the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scale to the (same) specified range.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  min the minimum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scales</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  max the maximum value of the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">- and </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-scales</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> (max == min)}</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setScale</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> min</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> max</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> size </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">-</span><span class="normal"> min</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">size </span><span class="symbol">==</span><span class="normal"> </span><span class="number">0.0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"the min and max are the same"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            xmin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">            xmax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">            ymin </span><span class="symbol">=</span><span class="normal"> min </span><span class="symbol">-</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">            ymax </span><span class="symbol">=</span><span class="normal"> max </span><span class="symbol">+</span><span class="normal"> BORDER </span><span class="symbol">*</span><span class="normal"> size</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// helper functions that scale from user coordinates to screen coordinates and back</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal">  </span><span class="function">scaleX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> width  </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">);</span><span class="normal"> </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal">  </span><span class="function">scaleY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> height </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">);</span><span class="normal"> </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> w</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> w </span><span class="symbol">*</span><span class="normal"> width  </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">abs</span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">);</span><span class="normal">  </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> h</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> h </span><span class="symbol">*</span><span class="normal"> height </span><span class="symbol">/</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">abs</span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">);</span><span class="normal">  </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal">   </span><span class="function">userX</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> xmin </span><span class="symbol">+</span><span class="normal"> x </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xmax </span><span class="symbol">-</span><span class="normal"> xmin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> width</span><span class="symbol">;</span><span class="normal">    </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal">   </span><span class="function">userY</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="keyword">return</span><span class="normal"> ymax </span><span class="symbol">-</span><span class="normal"> y </span><span class="symbol">*</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ymax </span><span class="symbol">-</span><span class="normal"> ymin</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">/</span><span class="normal"> height</span><span class="symbol">;</span><span class="normal">   </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Clears the screen to the default color (white).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">clear</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">clear</span><span class="symbol">(</span><span class="normal">DEFAULT_CLEAR_COLOR</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Clears the screen to the specified color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> color the color to make the background</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">clear</span><span class="symbol">(</span><span class="usertype">Color</span><span class="normal"> color</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">color</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">penColor</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the current pen radius.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current value of the pen radius</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">getPenRadius</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> penRadius</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen size to the default size (0.002).</span>
<span class="comment">     * The pen is circular, so that lines have rounded ends, and when you set the</span>
<span class="comment">     * pen radius and draw a point, you get a circle of the specified radius.</span>
<span class="comment">     * The pen radius is not affected by coordinate scaling.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenRadius</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setPenRadius</span><span class="symbol">(</span><span class="normal">DEFAULT_PEN_RADIUS</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the radius of the pen to the specified size.</span>
<span class="comment">     * The pen is circular, so that lines have rounded ends, and when you set the</span>
<span class="comment">     * pen radius and draw a point, you get a circle of the specified radius.</span>
<span class="comment">     * The pen radius is not affected by coordinate scaling.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  radius the radius of the pen</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> radius} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenRadius</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> radius</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">radius </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"pen radius must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        penRadius </span><span class="symbol">=</span><span class="normal"> radius</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">float</span><span class="normal"> scaledPenRadius </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">radius </span><span class="symbol">*</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">BasicStroke</span><span class="normal"> stroke </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BasicStroke</span><span class="symbol">(</span><span class="normal">scaledPenRadius</span><span class="symbol">,</span><span class="normal"> BasicStroke</span><span class="symbol">.</span><span class="normal">CAP_ROUND</span><span class="symbol">,</span><span class="normal"> BasicStroke</span><span class="symbol">.</span><span class="normal">JOIN_ROUND</span><span class="symbol">);</span>
<span class="normal">        </span><span class="comment">// BasicStroke stroke = new BasicStroke(scaledPenRadius);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setStroke</span><span class="symbol">(</span><span class="normal">stroke</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the current pen color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current pen color</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Color</span><span class="normal"> </span><span class="function">getPenColor</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> penColor</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Set the pen color to the default color (black).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">DEFAULT_PEN_COLOR</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen color to the specified color.</span>
<span class="comment">     * </span><span class="keyword">&lt;p&gt;</span>
<span class="comment">     * The predefined pen colors are</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.BLACK</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.BLUE</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.CYAN</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.DARK_GRAY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.GRAY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.GREEN</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.LIGHT_GRAY</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.MAGENTA</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.ORANGE</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">,</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.PINK</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.RED</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.WHITE</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">, and</span>
<span class="comment">     * </span><span class="keyword">&lt;tt&gt;</span><span class="comment">StdDraw.YELLOW</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> color the color to make the pen</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="usertype">Color</span><span class="normal"> color</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">color </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        penColor </span><span class="symbol">=</span><span class="normal"> color</span><span class="symbol">;</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setColor</span><span class="symbol">(</span><span class="normal">penColor</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the pen color to the specified RGB color.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  red the amount of red (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  green the amount of green (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  blue the amount of blue (between 0 and 255)</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> red}, {</span><span class="type">@code</span><span class="comment"> green},</span>
<span class="comment">     *         or {</span><span class="type">@code</span><span class="comment"> blue} is outside its prescribed range</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> red</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> green</span><span class="symbol">,</span><span class="normal"> </span><span class="type">int</span><span class="normal"> blue</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">red   </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> red   </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of red must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">green </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> green </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of green must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">blue  </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> blue  </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">256</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"amount of blue must be between 0 and 255"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">setPenColor</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> </span><span class="function">Color</span><span class="symbol">(</span><span class="normal">red</span><span class="symbol">,</span><span class="normal"> green</span><span class="symbol">,</span><span class="normal"> blue</span><span class="symbol">));</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the current font.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the current font</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Font</span><span class="normal"> </span><span class="function">getFont</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">return</span><span class="normal"> font</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the font to the default font (sans serif, 16 point).</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setFont</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">DEFAULT_FONT</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Sets the font to the specified value.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> font the font</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">setFont</span><span class="symbol">(</span><span class="usertype">Font</span><span class="normal"> font</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">font </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="normal">font </span><span class="symbol">=</span><span class="normal"> font</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing geometric shapes.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a line segment between (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">) and</span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x0 the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of one endpoint</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y0 the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of one endpoint</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x1 the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the other endpoint</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y1 the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the other endpoint</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">line</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x0</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y0</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> x1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y1</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Line2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x0</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y0</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x1</span><span class="symbol">),</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y1</span><span class="symbol">)));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws one pixel at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     * This method is private because pixels depend on the display.</span>
<span class="comment">     * To achieve the same effect, set the pen radius to 0 and call {</span><span class="type">@code</span><span class="comment"> point()}.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the pixel</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the pixel</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fillRect</span><span class="symbol">((</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">)),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">)),</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a point centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     * The point is a filled circle whose radius is equal to the pen radius.</span>
<span class="comment">     * To draw a single-pixel point, first set the pen radius to 0.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the point</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the point</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">point</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> r </span><span class="symbol">=</span><span class="normal"> penRadius</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">float</span><span class="normal"> scaledPenRadius </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">r </span><span class="symbol">*</span><span class="normal"> DEFAULT_SIZE</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// double ws = factorX(2*r);</span>
<span class="normal">        </span><span class="comment">// double hs = factorY(2*r);</span>
<span class="normal">        </span><span class="comment">// if (ws &lt;= 1 &amp;&amp; hs &lt;= 1) pixel(x, y);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scaledPenRadius </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> scaledPenRadius</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> scaledPenRadius</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span>
<span class="normal">                                                 scaledPenRadius</span><span class="symbol">,</span><span class="normal"> scaledPenRadius</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a circle of the specified radius, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  radius the radius of the circle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> radius} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">circle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> radius</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">radius </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"radius must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled circle of the specified radius, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  radius the radius of the circle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> radius} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledCircle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> radius</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">radius </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"radius must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws an ellipse with the specified semimajor and semiminor axes,</span>
<span class="comment">     * centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMajorAxis is the semimajor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMinorAxis is the semiminor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> semiMajorAxis}</span>
<span class="comment">     *         or {</span><span class="type">@code</span><span class="comment"> semiMinorAxis} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">ellipse</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMajorAxis</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMinorAxis</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">semiMajorAxis </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semimajor axis must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">semiMinorAxis </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semiminor axis must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMajorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMinorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws an ellipse with the specified semimajor and semiminor axes,</span>
<span class="comment">     * centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMajorAxis is the semimajor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  semiMinorAxis is the semiminor axis of the ellipse</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> semiMajorAxis}</span>
<span class="comment">     *         or {</span><span class="type">@code</span><span class="comment"> semiMinorAxis} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledEllipse</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMajorAxis</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> semiMinorAxis</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">semiMajorAxis </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semimajor axis must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">semiMinorAxis </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"ellipse semiminor axis must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMajorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">semiMinorAxis</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Ellipse2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a circular arc of the specified radius,</span>
<span class="comment">     * centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">), from angle1 to angle2 (in degrees).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  radius the radius of the circle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  angle1 the starting angle. 0 would mean an arc beginning at 3 o'clock.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  angle2 the angle at the end of the arc. For example, if</span>
<span class="comment">     *         you want a 90 degree arc, then angle2 should be angle1 + 90.</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> radius} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">arc</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> radius</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> angle2</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">radius </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"arc radius must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">while</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">angle2 </span><span class="symbol">&lt;</span><span class="normal"> angle1</span><span class="symbol">)</span><span class="normal"> angle2 </span><span class="symbol">+=</span><span class="normal"> </span><span class="number">360</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">radius</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Arc2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">,</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> angle2 </span><span class="symbol">-</span><span class="normal"> angle1</span><span class="symbol">,</span><span class="normal"> Arc2D</span><span class="symbol">.</span><span class="normal">OPEN</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a square of side length 2r, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfLength one half the length of any side of the square</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> halfLength} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">square</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfLength</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfLength </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half length must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfLength</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfLength</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled square of the specified size, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the square</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfLength one half the length of any side of the square</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if {</span><span class="type">@code</span><span class="comment"> halfLength} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledSquare</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfLength</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfLength </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half length must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfLength</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfLength</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a rectangle of the specified size, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfWidth one half the width of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfHeight one half the height of the rectangle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> halfWidth} or {</span><span class="type">@code</span><span class="comment"> halfHeight} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">rectangle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfWidth  </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half width must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfHeight </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half height must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a filled rectangle of the specified size, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the center of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfWidth one half the width of the rectangle</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  halfHeight one half the height of the rectangle</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> halfWidth} or {</span><span class="type">@code</span><span class="comment"> halfHeight} is negative</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledRectangle</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> halfHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfWidth  </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half width must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(!(</span><span class="normal">halfHeight </span><span class="symbol">&gt;=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">))</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"half height must be nonnegative"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="number">2</span><span class="symbol">*</span><span class="normal">halfHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="keyword">new</span><span class="normal"> Rectangle2D</span><span class="symbol">.</span><span class="function">Double</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> ws</span><span class="symbol">,</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a polygon with the vertices </span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">),</span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">), ...,</span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;&lt;em&gt;</span><span class="comment">n</span><span class="keyword">&lt;/em&gt;</span><span class="preproc">&amp;minus;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;&lt;em&gt;</span><span class="comment">n</span><span class="keyword">&lt;/em&gt;</span><span class="preproc">&amp;minus;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x an array of all the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates of the polygon</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y an array of all the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates of the polygon</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless {</span><span class="type">@code</span><span class="comment"> x[]} and {</span><span class="type">@code</span><span class="comment"> y[]}</span>
<span class="comment">     *         are of the same length</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">polygon</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n1 </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n2 </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n1 </span><span class="symbol">!=</span><span class="normal"> n2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"arrays must be of the same length"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> n1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">GeneralPath</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">GeneralPath</span><span class="symbol">();</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">moveTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">lineTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]));</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">closePath</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">draw</span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws a polygon with the vertices </span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">0</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">),</span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">), ...,</span>
<span class="comment">     * (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;&lt;em&gt;</span><span class="comment">n</span><span class="keyword">&lt;/em&gt;</span><span class="preproc">&amp;minus;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;&lt;sub&gt;&lt;em&gt;</span><span class="comment">n</span><span class="keyword">&lt;/em&gt;</span><span class="preproc">&amp;minus;</span><span class="comment">1</span><span class="keyword">&lt;/sub&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x an array of all the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates of the polygon</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y an array of all the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinates of the polygon</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException unless {</span><span class="type">@code</span><span class="comment"> x[]} and {</span><span class="type">@code</span><span class="comment"> y[]}</span>
<span class="comment">     *         are of the same length</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">filledPolygon</span><span class="symbol">(</span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">x </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">y </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n1 </span><span class="symbol">=</span><span class="normal"> x</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n2 </span><span class="symbol">=</span><span class="normal"> y</span><span class="symbol">.</span><span class="normal">length</span><span class="symbol">;</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">n1 </span><span class="symbol">!=</span><span class="normal"> n2</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"arrays must be of the same length"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> n </span><span class="symbol">=</span><span class="normal"> n1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="usertype">GeneralPath</span><span class="normal"> path </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">GeneralPath</span><span class="symbol">();</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">moveTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="number">0</span><span class="symbol">]));</span>
<span class="normal">        </span><span class="keyword">for</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="normal"> i </span><span class="symbol">=</span><span class="normal"> </span><span class="number">0</span><span class="symbol">;</span><span class="normal"> i </span><span class="symbol">&lt;</span><span class="normal"> n</span><span class="symbol">;</span><span class="normal"> i</span><span class="symbol">++)</span>
<span class="normal">            path</span><span class="symbol">.</span><span class="function">lineTo</span><span class="symbol">((</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">[</span><span class="normal">i</span><span class="symbol">]));</span>
<span class="normal">        path</span><span class="symbol">.</span><span class="function">closePath</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">fill</span><span class="symbol">(</span><span class="normal">path</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing images.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">// get an image from the given filename</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="usertype">Image</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">filename </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>

<span class="normal">        </span><span class="comment">// to read from file</span>
<span class="normal">        </span><span class="usertype">ImageIcon</span><span class="normal"> icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// try to read from URL</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">icon </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">.</span><span class="function">getImageLoadStatus</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> MediaTracker</span><span class="symbol">.</span><span class="normal">COMPLETE</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">URL</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">                icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">Exception</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="comment">/* not a url */</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// in case file is inside a .jar (classpath relative to StdDraw)</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">icon </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">.</span><span class="function">getImageLoadStatus</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> MediaTracker</span><span class="symbol">.</span><span class="normal">COMPLETE</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="keyword">class</span><span class="symbol">.</span><span class="function">getResource</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span>
<span class="normal">                icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// in case file is inside a .jar (classpath relative to root of jar)</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">((</span><span class="normal">icon </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">icon</span><span class="symbol">.</span><span class="function">getImageLoadStatus</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">!=</span><span class="normal"> MediaTracker</span><span class="symbol">.</span><span class="normal">COMPLETE</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">URL</span><span class="normal"> url </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="keyword">class</span><span class="symbol">.</span><span class="function">getResource</span><span class="symbol">(</span><span class="string">"/"</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">url </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" not found"</span><span class="symbol">);</span>
<span class="normal">            icon </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">ImageIcon</span><span class="symbol">(</span><span class="normal">url</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">return</span><span class="normal"> icon</span><span class="symbol">.</span><span class="function">getImage</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws the specified image centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     * The supported image formats are JPEG, PNG, and GIF.</span>
<span class="comment">     * As an optimization, the picture is cached, so there is no performance</span>
<span class="comment">     * penalty for redrawing the same image multiple times (e.g., in an animation).</span>
<span class="comment">     * However, if you change the picture file after drawing it, subsequent</span>
<span class="comment">     * calls will draw the original picture.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image filename is invalid</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws the specified image centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">),</span>
<span class="comment">     * rotated given number of degrees.</span>
<span class="comment">     * The supported image formats are JPEG, PNG, and GIF.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image filename is invalid</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getWidth</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> image</span><span class="symbol">.</span><span class="function">getHeight</span><span class="symbol">(</span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>

<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws the specified image centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">),</span>
<span class="comment">     * rescaled to the specified bounding box.</span>
<span class="comment">     * The supported image formats are JPEG, PNG, and GIF.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  scaledWidth the width of the scaled image in pixels</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  scaledHeight the height of the scaled image in pixels</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> scaledWidth}</span>
<span class="comment">     *         or {</span><span class="type">@code</span><span class="comment"> scaledHeight} is negative</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image filename is invalid</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> scaledWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> scaledHeight</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scaledWidth </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"width is negative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> scaledWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scaledHeight </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"height is negative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> scaledHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="normal">scaledWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="normal">scaledHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ws</span><span class="symbol">),</span>
<span class="normal">                                       </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">hs</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Draws the specified image centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">), rotated</span>
<span class="comment">     * given number of degrees, and rescaled to the specified bounding box.</span>
<span class="comment">     * The supported image formats are JPEG, PNG, and GIF.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the image</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the image/picture, e.g., "ball.gif"</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  scaledWidth the width of the scaled image in pixels</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  scaledHeight the height of the scaled image in pixels</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if either {</span><span class="type">@code</span><span class="comment"> scaledWidth}</span>
<span class="comment">     *         or {</span><span class="type">@code</span><span class="comment"> scaledHeight} is negative</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> IllegalArgumentException if the image filename is invalid</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">picture</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> scaledWidth</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> scaledHeight</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scaledWidth </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"width is negative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> scaledWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">scaledHeight </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"height is negative: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> scaledHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">Image</span><span class="normal"> image </span><span class="symbol">=</span><span class="normal"> </span><span class="function">getImage</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorX</span><span class="symbol">(</span><span class="normal">scaledWidth</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">factorY</span><span class="symbol">(</span><span class="normal">scaledHeight</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="normal"> </span><span class="symbol">||</span><span class="normal"> hs </span><span class="symbol">&lt;</span><span class="normal"> </span><span class="number">0</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">IllegalArgumentException</span><span class="symbol">(</span><span class="string">"image "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> filename </span><span class="symbol">+</span><span class="normal"> </span><span class="string">" is corrupt"</span><span class="symbol">);</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ws </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="normal"> </span><span class="symbol">&amp;&amp;</span><span class="normal"> hs </span><span class="symbol">&lt;=</span><span class="normal"> </span><span class="number">1</span><span class="symbol">)</span><span class="normal"> </span><span class="function">pixel</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>

<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">image</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">-</span><span class="normal"> hs</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">ws</span><span class="symbol">),</span>
<span class="normal">                                   </span><span class="symbol">(</span><span class="type">int</span><span class="symbol">)</span><span class="normal"> Math</span><span class="symbol">.</span><span class="function">round</span><span class="symbol">(</span><span class="normal">hs</span><span class="symbol">),</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>

<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Drawing text.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Write the given text string in the current font, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  text the text to write</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">text</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">text </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">font</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">FontMetrics</span><span class="normal"> metrics </span><span class="symbol">=</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">getFontMetrics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">stringWidth</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">getDescent</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawString</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">/</span><span class="number">2.0</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">+</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Write the given text string in the current font, centered at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">) and</span>
<span class="comment">     * rotated by the specified number of degrees.</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the center </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  text the text to write</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  degrees is the number of degrees to rotate counterclockwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">text</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> degrees</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">text </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(-</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">        </span><span class="function">text</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> text</span><span class="symbol">);</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">rotate</span><span class="symbol">(</span><span class="normal">Math</span><span class="symbol">.</span><span class="function">toRadians</span><span class="symbol">(+</span><span class="normal">degrees</span><span class="symbol">),</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> ys</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Write the given text string in the current font, left-aligned at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  text the text</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">textLeft</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">text </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">font</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">FontMetrics</span><span class="normal"> metrics </span><span class="symbol">=</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">getFontMetrics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">getDescent</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawString</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> xs</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">+</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Write the given text string in the current font, right-aligned at (</span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">, </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  x the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  y the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the text</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  text the text to write</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">textRight</span><span class="symbol">(</span><span class="type">double</span><span class="normal"> x</span><span class="symbol">,</span><span class="normal"> </span><span class="type">double</span><span class="normal"> y</span><span class="symbol">,</span><span class="normal"> </span><span class="usertype">String</span><span class="normal"> text</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">text </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">setFont</span><span class="symbol">(</span><span class="normal">font</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">FontMetrics</span><span class="normal"> metrics </span><span class="symbol">=</span><span class="normal"> offscreen</span><span class="symbol">.</span><span class="function">getFontMetrics</span><span class="symbol">();</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> xs </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleX</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="normal"> ys </span><span class="symbol">=</span><span class="normal"> </span><span class="function">scaleY</span><span class="symbol">(</span><span class="normal">y</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> ws </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">stringWidth</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">int</span><span class="normal"> hs </span><span class="symbol">=</span><span class="normal"> metrics</span><span class="symbol">.</span><span class="function">getDescent</span><span class="symbol">();</span>
<span class="normal">        offscreen</span><span class="symbol">.</span><span class="function">drawString</span><span class="symbol">(</span><span class="normal">text</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">xs </span><span class="symbol">-</span><span class="normal"> ws</span><span class="symbol">),</span><span class="normal"> </span><span class="symbol">(</span><span class="type">float</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">ys </span><span class="symbol">+</span><span class="normal"> hs</span><span class="symbol">));</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>



<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Display on screen, pause for t milliseconds, and turn on</span>
<span class="comment">     * </span><span class="keyword">&lt;em&gt;</span><span class="comment">animation mode</span><span class="keyword">&lt;/em&gt;</span><span class="comment">: subsequent calls to</span>
<span class="comment">     * drawing methods such as {</span><span class="type">@code</span><span class="comment"> line()}, {</span><span class="type">@code</span><span class="comment"> circle()}, and {</span><span class="type">@code</span><span class="comment"> square()}</span>
<span class="comment">     * will not be displayed on screen until the next call to {</span><span class="type">@code</span><span class="comment"> show()}.</span>
<span class="comment">     * This is useful for producing animations (clear the screen, draw a bunch of shapes,</span>
<span class="comment">     * display on screen for a fixed amount of time, and repeat). It also speeds up</span>
<span class="comment">     * drawing a huge number of shapes (call {</span><span class="type">@code</span><span class="comment"> show(0)} to defer drawing</span>
<span class="comment">     * on screen, draw the shapes, and call {</span><span class="type">@code</span><span class="comment"> show(0)} to display them all</span>
<span class="comment">     * on screen at once).</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment"> t number of milliseconds</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> t</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="comment">// sleep until the next time we're allowed to draw</span>
<span class="normal">        </span><span class="type">long</span><span class="normal"> millis </span><span class="symbol">=</span><span class="normal"> System</span><span class="symbol">.</span><span class="function">currentTimeMillis</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">millis </span><span class="symbol">&lt;</span><span class="normal"> nextDraw</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                Thread</span><span class="symbol">.</span><span class="function">sleep</span><span class="symbol">(</span><span class="normal">nextDraw </span><span class="symbol">-</span><span class="normal"> millis</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">InterruptedException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Error sleeping"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            millis </span><span class="symbol">=</span><span class="normal"> nextDraw</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>

<span class="normal">        </span><span class="comment">// when are we allowed to draw again</span>
<span class="normal">        nextDraw </span><span class="symbol">=</span><span class="normal"> millis </span><span class="symbol">+</span><span class="normal"> t</span><span class="symbol">;</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Display on-screen and turn off animation mode:</span>
<span class="comment">     * subsequent calls to</span>
<span class="comment">     * drawing methods such as {</span><span class="type">@code</span><span class="comment"> line()}, {</span><span class="type">@code</span><span class="comment"> circle()},</span>
<span class="comment">     * and {</span><span class="type">@code</span><span class="comment"> square()} will be displayed on screen when called.</span>
<span class="comment">     * This is the default.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">show</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        defer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        nextDraw </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">-</span><span class="number">1</span><span class="symbol">;</span>
<span class="normal">        </span><span class="function">draw</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">// draw onscreen if defer is false</span>
<span class="normal">    </span><span class="keyword">private</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">draw</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">defer</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">return</span><span class="symbol">;</span>
<span class="normal">        onscreen</span><span class="symbol">.</span><span class="function">drawImage</span><span class="symbol">(</span><span class="normal">offscreenImage</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">        frame</span><span class="symbol">.</span><span class="function">repaint</span><span class="symbol">();</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Save drawing to a file.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Saves the drawing to using the specified filename.</span>
<span class="comment">     * The supported image formats are JPEG and PNG;</span>
<span class="comment">     * the filename suffix must be </span><span class="keyword">&lt;tt&gt;</span><span class="comment">.jpg</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> or </span><span class="keyword">&lt;tt&gt;</span><span class="comment">.png</span><span class="keyword">&lt;/tt&gt;</span><span class="comment">.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  filename the name of the file with one of the required suffixes</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">save</span><span class="symbol">(</span><span class="usertype">String</span><span class="normal"> filename</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">filename </span><span class="symbol">==</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NullPointerException</span><span class="symbol">();</span>
<span class="normal">        </span><span class="usertype">File</span><span class="normal"> file </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">File</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> suffix </span><span class="symbol">=</span><span class="normal"> filename</span><span class="symbol">.</span><span class="function">substring</span><span class="symbol">(</span><span class="normal">filename</span><span class="symbol">.</span><span class="function">lastIndexOf</span><span class="symbol">(</span><span class="string">'.'</span><span class="symbol">)</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> </span><span class="number">1</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// png files</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">toLowerCase</span><span class="symbol">().</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"png"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                ImageIO</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">onscreenImage</span><span class="symbol">,</span><span class="normal"> suffix</span><span class="symbol">,</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="comment">// need to change from ARGB to RGB for JPEG</span>
<span class="normal">        </span><span class="comment">// reference: http://archives.java.sun.com/cgi-bin/wa?A2=ind0404&amp;L=java2d-interest&amp;D=0&amp;P=2727</span>
<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">suffix</span><span class="symbol">.</span><span class="function">toLowerCase</span><span class="symbol">().</span><span class="function">equals</span><span class="symbol">(</span><span class="string">"jpg"</span><span class="symbol">))</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="usertype">WritableRaster</span><span class="normal"> raster </span><span class="symbol">=</span><span class="normal"> onscreenImage</span><span class="symbol">.</span><span class="function">getRaster</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">WritableRaster</span><span class="normal"> newRaster</span><span class="symbol">;</span>
<span class="normal">            newRaster </span><span class="symbol">=</span><span class="normal"> raster</span><span class="symbol">.</span><span class="function">createWritableChild</span><span class="symbol">(</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> width</span><span class="symbol">,</span><span class="normal"> height</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="type">int</span><span class="symbol">[]</span><span class="normal"> </span><span class="cbracket">{</span><span class="number">0</span><span class="symbol">,</span><span class="normal"> </span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">2</span><span class="cbracket">}</span><span class="symbol">);</span>
<span class="normal">            </span><span class="usertype">DirectColorModel</span><span class="normal"> cm </span><span class="symbol">=</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">DirectColorModel</span><span class="symbol">)</span><span class="normal"> onscreenImage</span><span class="symbol">.</span><span class="function">getColorModel</span><span class="symbol">();</span>
<span class="normal">            </span><span class="usertype">DirectColorModel</span><span class="normal"> newCM </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">DirectColorModel</span><span class="symbol">(</span><span class="normal">cm</span><span class="symbol">.</span><span class="function">getPixelSize</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getRedMask</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getGreenMask</span><span class="symbol">(),</span>
<span class="normal">                                                          cm</span><span class="symbol">.</span><span class="function">getBlueMask</span><span class="symbol">());</span>
<span class="normal">            </span><span class="usertype">BufferedImage</span><span class="normal"> rgbBuffer </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">BufferedImage</span><span class="symbol">(</span><span class="normal">newCM</span><span class="symbol">,</span><span class="normal"> newRaster</span><span class="symbol">,</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">,</span><span class="normal">  </span><span class="keyword">null</span><span class="symbol">);</span>
<span class="normal">            </span><span class="keyword">try</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                ImageIO</span><span class="symbol">.</span><span class="function">write</span><span class="symbol">(</span><span class="normal">rgbBuffer</span><span class="symbol">,</span><span class="normal"> suffix</span><span class="symbol">,</span><span class="normal"> file</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">catch</span><span class="normal"> </span><span class="symbol">(</span><span class="usertype">IOException</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                e</span><span class="symbol">.</span><span class="function">printStackTrace</span><span class="symbol">();</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">        </span><span class="cbracket">}</span>

<span class="normal">        </span><span class="keyword">else</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            System</span><span class="symbol">.</span><span class="normal">out</span><span class="symbol">.</span><span class="function">println</span><span class="symbol">(</span><span class="string">"Invalid image file type: "</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> suffix</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">actionPerformed</span><span class="symbol">(</span><span class="usertype">ActionEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="usertype">FileDialog</span><span class="normal"> chooser </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">FileDialog</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">frame</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"Use a .png or .jpg extension"</span><span class="symbol">,</span><span class="normal"> FileDialog</span><span class="symbol">.</span><span class="normal">SAVE</span><span class="symbol">);</span>
<span class="normal">        chooser</span><span class="symbol">.</span><span class="function">setVisible</span><span class="symbol">(</span><span class="keyword">true</span><span class="symbol">);</span>
<span class="normal">        </span><span class="usertype">String</span><span class="normal"> filename </span><span class="symbol">=</span><span class="normal"> chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">();</span>
<span class="normal">        </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">filename </span><span class="symbol">!=</span><span class="normal"> </span><span class="keyword">null</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            StdDraw</span><span class="symbol">.</span><span class="function">save</span><span class="symbol">(</span><span class="normal">chooser</span><span class="symbol">.</span><span class="function">getDirectory</span><span class="symbol">()</span><span class="normal"> </span><span class="symbol">+</span><span class="normal"> File</span><span class="symbol">.</span><span class="normal">separator </span><span class="symbol">+</span><span class="normal"> chooser</span><span class="symbol">.</span><span class="function">getFile</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Mouse interactions.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the mouse is being pressed.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the mouse is being pressed; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">mousePressed</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mousePressed</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the mouse.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the </span><span class="keyword">&lt;em&gt;</span><span class="comment">x</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the mouse</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">mouseX</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mouseX</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the mouse.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;em&gt;</span><span class="comment">y</span><span class="keyword">&lt;/em&gt;</span><span class="comment">-coordinate of the mouse</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">double</span><span class="normal"> </span><span class="function">mouseY</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> mouseY</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseClicked</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseEntered</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseExited</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mousePressed</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">            mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">true</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseReleased</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mousePressed </span><span class="symbol">=</span><span class="normal"> </span><span class="keyword">false</span><span class="symbol">;</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseDragged</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal">  </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">mouseMoved</span><span class="symbol">(</span><span class="usertype">MouseEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">mouseLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            mouseX </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userX</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getX</span><span class="symbol">());</span>
<span class="normal">            mouseY </span><span class="symbol">=</span><span class="normal"> StdDraw</span><span class="symbol">.</span><span class="function">userY</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getY</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">   </span><span class="comment">/***************************************************************************</span>
<span class="comment">    *  Keyboard interactions.</span>
<span class="comment">    ***************************************************************************/</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the user has typed a key (that has not yet been processed).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if the user has typed a key (that has not yet been processed</span>
<span class="comment">     *         by {</span><span class="type">@link</span><span class="comment"> #nextKeyTyped()}; </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">hasNextKeyTyped</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> </span><span class="symbol">!</span><span class="normal">keysTyped</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns the next key that was typed by the user (that your program has not already processed).</span>
<span class="comment">     * This method should be preceded by a call to {</span><span class="type">@link</span><span class="comment"> #hasNextKeyTyped()} to ensure</span>
<span class="comment">     * that there is a next key to process.</span>
<span class="comment">     * This method returns a Unicode character corresponding to the key</span>
<span class="comment">     * typed (such as {</span><span class="type">@code</span><span class="comment"> 'a'} or {</span><span class="type">@code</span><span class="comment"> 'A'}).</span>
<span class="comment">     * It cannot identify action keys (such as F1 and arrow keys)</span>
<span class="comment">     * or modifier keys (such as control).</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> the next key typed by the user (that your program has not already processed).</span>
<span class="comment">     * </span><span class="type">@throws</span><span class="comment"> NoSuchElementException if there is no remaining key</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">char</span><span class="normal"> </span><span class="function">nextKeyTyped</span><span class="symbol">()</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">if</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keysTyped</span><span class="symbol">.</span><span class="function">isEmpty</span><span class="symbol">())</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">                </span><span class="keyword">throw</span><span class="normal"> </span><span class="keyword">new</span><span class="normal"> </span><span class="function">NoSuchElementException</span><span class="symbol">(</span><span class="string">"your program has already processed all keystrokes"</span><span class="symbol">);</span>
<span class="normal">            </span><span class="cbracket">}</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> keysTyped</span><span class="symbol">.</span><span class="function">removeLast</span><span class="symbol">();</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Returns true if the given key is being pressed.</span>
<span class="comment">     * </span><span class="keyword">&lt;p&gt;</span>
<span class="comment">     * This method takes the keycode (corresponding to a physical key)</span>
<span class="comment">    *  as an argument. It can handle action keys</span>
<span class="comment">     * (such as F1 and arrow keys) and modifier keys (such as shift and control).</span>
<span class="comment">     * See {</span><span class="type">@link</span><span class="comment"> KeyEvent} for a description of key codes.</span>
<span class="comment">     *</span>
<span class="comment">     * </span><span class="type">@param</span><span class="comment">  keycode the key to check if it is being pressed</span>
<span class="comment">     * </span><span class="type">@return</span><span class="comment"> </span><span class="keyword">&lt;tt&gt;</span><span class="comment">true</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> if {</span><span class="type">@code</span><span class="comment"> keycode} is currently being pressed;</span>
<span class="comment">     *         </span><span class="keyword">&lt;tt&gt;</span><span class="comment">false</span><span class="keyword">&lt;/tt&gt;</span><span class="comment"> otherwise</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">boolean</span><span class="normal"> </span><span class="function">isKeyPressed</span><span class="symbol">(</span><span class="type">int</span><span class="normal"> keycode</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            </span><span class="keyword">return</span><span class="normal"> keysDown</span><span class="symbol">.</span><span class="function">contains</span><span class="symbol">(</span><span class="normal">keycode</span><span class="symbol">);</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>


<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyTyped</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysTyped</span><span class="symbol">.</span><span class="function">addFirst</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyChar</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyPressed</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysDown</span><span class="symbol">.</span><span class="function">add</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * This method cannot be called directly.</span>
<span class="comment">     */</span>
<span class="normal">    @Override</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">keyReleased</span><span class="symbol">(</span><span class="usertype">KeyEvent</span><span class="normal"> e</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        </span><span class="keyword">synchronized</span><span class="normal"> </span><span class="symbol">(</span><span class="normal">keyLock</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">            keysDown</span><span class="symbol">.</span><span class="function">remove</span><span class="symbol">(</span><span class="normal">e</span><span class="symbol">.</span><span class="function">getKeyCode</span><span class="symbol">());</span>
<span class="normal">        </span><span class="cbracket">}</span>
<span class="normal">    </span><span class="cbracket">}</span>




<span class="normal">    </span><span class="comment">/**</span>
<span class="comment">     * Test client.</span>
<span class="comment">     */</span>
<span class="normal">    </span><span class="keyword">public</span><span class="normal"> </span><span class="keyword">static</span><span class="normal"> </span><span class="type">void</span><span class="normal"> </span><span class="function">main</span><span class="symbol">(</span><span class="normal">String</span><span class="symbol">[]</span><span class="normal"> args</span><span class="symbol">)</span><span class="normal"> </span><span class="cbracket">{</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">square</span><span class="symbol">(.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">filledSquare</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">circle</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">);</span>

<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">BOOK_RED</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">(.</span><span class="number">02</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">arc</span><span class="symbol">(.</span><span class="number">8</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="number">200</span><span class="symbol">,</span><span class="normal"> </span><span class="number">45</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// draw a blue diamond</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenRadius</span><span class="symbol">();</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">BOOK_BLUE</span><span class="symbol">);</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> x </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        </span><span class="type">double</span><span class="symbol">[]</span><span class="normal"> y </span><span class="symbol">=</span><span class="normal"> </span><span class="cbracket">{</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">3</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">2</span><span class="symbol">,</span><span class="normal"> </span><span class="symbol">.</span><span class="number">1</span><span class="normal"> </span><span class="cbracket">}</span><span class="symbol">;</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">filledPolygon</span><span class="symbol">(</span><span class="normal">x</span><span class="symbol">,</span><span class="normal"> y</span><span class="symbol">);</span>

<span class="normal">        </span><span class="comment">// text</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">BLACK</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">text</span><span class="symbol">(</span><span class="number">0.2</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.5</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"black text"</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">setPenColor</span><span class="symbol">(</span><span class="normal">StdDraw</span><span class="symbol">.</span><span class="normal">WHITE</span><span class="symbol">);</span>
<span class="normal">        StdDraw</span><span class="symbol">.</span><span class="function">text</span><span class="symbol">(</span><span class="number">0.8</span><span class="symbol">,</span><span class="normal"> </span><span class="number">0.8</span><span class="symbol">,</span><span class="normal"> </span><span class="string">"white text"</span><span class="symbol">);</span>
<span class="normal">    </span><span class="cbracket">}</span>

<span class="cbracket">}</span>

<span class="comment">/******************************************************************************</span>
<span class="comment"> *  Copyright 2002-2015, Robert Sedgewick and Kevin Wayne.</span>
<span class="comment"> *</span>
<span class="comment"> *  This file is part of algs4.jar, which accompanies the textbook</span>
<span class="comment"> *</span>
<span class="comment"> *      Algorithms, 4th edition by Robert Sedgewick and Kevin Wayne,</span>
<span class="comment"> *      Addison-Wesley Professional, 2011, ISBN 0-321-57351-X.</span>
<span class="comment"> *      </span><span class="url">http://algs4.cs.princeton.edu</span>
<span class="comment"> *</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is free software: you can redistribute it and/or modify</span>
<span class="comment"> *  it under the terms of the GNU General Public License as published by</span>
<span class="comment"> *  the Free Software Foundation, either version 3 of the License, or</span>
<span class="comment"> *  (at your option) any later version.</span>
<span class="comment"> *</span>
<span class="comment"> *  algs4.jar is distributed in the hope that it will be useful,</span>
<span class="comment"> *  but WITHOUT ANY WARRANTY; without even the implied warranty of</span>
<span class="comment"> *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the</span>
<span class="comment"> *  GNU General Public License for more details.</span>
<span class="comment"> *</span>
<span class="comment"> *  You should have received a copy of the GNU General Public License</span>
<span class="comment"> *  along with algs4.jar.  If not, see </span><span class="url">http://www.gnu.org/licenses.</span>
<span class="comment"> ******************************************************************************/</span>
</tt></pre>

<script type="text/javascript">
var gaJsHost = (("https:" == document.location.protocol) ? "https://ssl." : "http://www.");
document.write(unescape("%3Cscript src='" + gaJsHost + "google-analytics.com/ga.js' type='text/javascript'%3E%3C/script%3E"));
</script>
<script type="text/javascript">
try {
var pageTracker = _gat._getTracker("UA-10811519-2");
pageTracker._trackPageview();
} catch(err) {}</script>

</body>

<p><br><address><small>
Last updated: Sat Oct 24 19:31:17 EDT 2015.
</small></address>

</html>
